<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNum">
        <el-input v-model="queryParams.orderNum" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入订单名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否结算" prop="profferStatus">
        <el-select v-model="queryParams.profferStatus" placeholder="请选择是否结算" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.JKY_IS_SHOW)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.JKY_ORDER_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker v-model="queryParams.payTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="发货时间" prop="deliverTime">
        <el-date-picker v-model="queryParams.deliverTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="确认收货时间" prop="finishTime">
        <el-date-picker v-model="queryParams.finishTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="申请退款时间" prop="refundTime">
        <el-date-picker v-model="queryParams.refundTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="退款成功时间" prop="refundSuccessTime">
        <el-date-picker v-model="queryParams.refundSuccessTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="拒绝退款时间" prop="refundFailTime">
        <el-date-picker v-model="queryParams.refundFailTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="支付方式" prop="payMethod">
        <el-select v-model="queryParams.payMethod" placeholder="请选择支付方式" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.JKY_PAY_METHOD)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="商城类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择商城类型" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.JKY_MALL_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-input v-model="queryParams.supplierId" placeholder="请输入供应商组ID 0代表无" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['supplier:order:create']">新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['supplier:order:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="orderNum" />
      <el-table-column label="订单名称" align="center" prop="title" />
      <el-table-column label="总价" align="center" prop="allprice" >
        <template v-slot="scope">
          {{'￥' + scope.row.allprice / 100 }}
        </template>
      </el-table-column>
      <el-table-column label="收件人" align="center" prop="consignee" />
      <el-table-column label="联系电话" align="center" prop="contactNumber" />
      <el-table-column label="收货地址" align="center" prop="address" />
      <el-table-column label="邮政编码" align="center" prop="postcode" />
      <el-table-column label="订单状态" align="center" prop="status" >
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.JKY_ORDER_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payMethod" >
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.JKY_PAY_METHOD" :value="scope.row.payMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="商城类型" align="center" prop="type" >
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.JKY_MALL_TYPE" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="供应商组" align="center" prop="supplierName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="small"
            @click="handleViewOrder(scope.$index, scope.row)"
          >查看订单</el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            v-show="scope.row.status===5">删除订单</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import { createOrder, updateOrder, deleteOrder, getOrder, getOrderPage, exportOrderExcel } from "@/api/supplier/order";

export default {
  name: "Order",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        userId: null,
        orderNum: null,
        title: null,
        allprice: null,
        givePoint: null,
        proffer: null,
        profferStatus: null,
        deductionPoint: null,
        province: null,
        city: null,
        county: null,
        address: null,
        company: null,
        consignee: null,
        contactNumber: null,
        postcode: null,
        logistics: null,
        expressNumber: null,
        remark: null,
        status: null,
        createTime: [],
        payTime: [],
        deliverTime: [],
        finishTime: [],
        commentTime: [],
        refundTime: [],
        refundSuccessTime: [],
        refundFailTime: [],
        payMethod: null,
        point: null,
        consumerCode: null,
        drawbackReason: null,
        drawbackImg: null,
        drawbackRefuseReason: null,
        refundExpressNumber: null,
        isDelay: null,
        cancelTime: [],
        type: null,
        supplierId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [{ required: true, message: "用户ID不能为空", trigger: "blur" }],
        orderNum: [{ required: true, message: "订单号不能为空", trigger: "blur" }],
        allprice: [{ required: true, message: "总价 精确到分不能为空", trigger: "blur" }],
        proffer: [{ required: true, message: "分成不能为空", trigger: "blur" }],
        profferStatus: [{ required: true, message: "1=未结算2=结算不能为空", trigger: "blur" }],
        status: [{ required: true, message: "订单状态 1未支付 2已支付 3已发货 4已确认收货、未评价 5已确认收货、已评价 6申请退款 7退款成功 8拒绝退款不能为空", trigger: "blur" }],
        isDelay: [{ required: true, message: "是否延迟收货 Y是 N否不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型1自营商城3寄售商城不能为空", trigger: "change" }],
        supplierId: [{ required: true, message: "供应商组ID 0代表无不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleViewOrder(index, row){
      this.$router.push({name:'OrderDetail',query:{id:row.id}})
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getOrderPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        orderNum: undefined,
        title: undefined,
        allprice: undefined,
        givePoint: undefined,
        proffer: undefined,
        profferStatus: undefined,
        deductionPoint: undefined,
        province: undefined,
        city: undefined,
        county: undefined,
        address: undefined,
        company: undefined,
        consignee: undefined,
        contactNumber: undefined,
        postcode: undefined,
        logistics: undefined,
        expressNumber: undefined,
        remark: undefined,
        status: undefined,
        payTime: undefined,
        deliverTime: undefined,
        finishTime: undefined,
        commentTime: undefined,
        refundTime: undefined,
        refundSuccessTime: undefined,
        refundFailTime: undefined,
        payMethod: undefined,
        point: undefined,
        consumerCode: undefined,
        drawbackReason: undefined,
        drawbackImg: undefined,
        drawbackRefuseReason: undefined,
        refundExpressNumber: undefined,
        isDelay: undefined,
        cancelTime: undefined,
        type: undefined,
        supplierId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateOrder(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createOrder(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除订单编号为"' + id + '"的数据项?').then(function() {
          return deleteOrder(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有订单数据项?').then(() => {
          this.exportLoading = true;
          return exportOrderExcel(params);
        }).then(response => {
          this.$download.excel(response, '订单.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
